import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
var _excluded = ["tenantId"],
    _excluded2 = ["tenantId"],
    _excluded3 = ["tenantId"],
    _excluded4 = ["tenantId"],
    _excluded5 = ["clientId"],
    _excluded6 = ["clientId"];

/**
 *  客户端
 * @date: 2018-12-24


 */
import request from 'utils/request';
import { CPAAS_IAM } from 'utils/config';
import { getCurrentOrganizationId, isTenantRoleLevel, parseParameters } from 'utils/utils';
/**
 * 请求API前缀
 * @type {string}
 */

var prefix = "".concat(CPAAS_IAM, "/v1");
var currentOrganizationId = getCurrentOrganizationId();

function clientApi() {
  return isTenantRoleLevel() ? "".concat(currentOrganizationId, "/clients") : "clients";
}

function client() {
  return isTenantRoleLevel() ? "".concat(currentOrganizationId, "/") : "";
}

function visitRole() {
  return isTenantRoleLevel() ? "".concat(currentOrganizationId, "/clients/access-roles") : "clients/access-roles";
}
/**
 * 查询client列表数据
 * @async
 * @function fetchClientList
 * @param {object} params - 查询条件
 */


export function fetchClientList(_x) {
  return _fetchClientList.apply(this, arguments);
}
/**
 * 查询clien详情数据
 * @async
 * @function fetchDetail
 * @param {object} params - 查询条件
 */

function _fetchClientList() {
  _fetchClientList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
    var param, tenantId, others;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            param = parseParameters(params);
            tenantId = param.tenantId, others = _objectWithoutProperties(param, _excluded);
            return _context.abrupt("return", request("".concat(prefix, "/").concat(clientApi()), {
              method: 'GET',
              query: others
            }));

          case 3:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _fetchClientList.apply(this, arguments);
}

export function fetchDetail(_x2) {
  return _fetchDetail.apply(this, arguments);
}
/**
 * 校验
 * @async
 * @function checkClient
 * @param {object} params - 创建参数
 */

function _fetchDetail() {
  _fetchDetail = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    var clientId;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            clientId = params.clientId;
            return _context2.abrupt("return", request("".concat(prefix, "/").concat(clientApi(), "/").concat(clientId), {
              method: 'GET'
            }));

          case 2:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _fetchDetail.apply(this, arguments);
}

export function checkClient(_x3) {
  return _checkClient.apply(this, arguments);
}
/**
 * 新建
 * @async
 * @function createClient
 * @param {object} params - 创建参数
 */

function _checkClient() {
  _checkClient = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    var tenantId, others;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            tenantId = params.tenantId, others = _objectWithoutProperties(params, _excluded2);
            return _context3.abrupt("return", request("".concat(prefix, "/").concat(clientApi(), "/check"), {
              method: 'POST',
              responseType: 'text',
              body: others
            }));

          case 2:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _checkClient.apply(this, arguments);
}

export function createClient(_x4) {
  return _createClient.apply(this, arguments);
}
/**
 * 更新
 * @async
 * @function updateClient
 * @param {object} params - 参数
 */

function _createClient() {
  _createClient = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    var tenantId, others;
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            tenantId = params.tenantId, others = _objectWithoutProperties(params, _excluded3);
            return _context4.abrupt("return", request("".concat(prefix, "/").concat(clientApi()), {
              method: 'POST',
              body: others
            }));

          case 2:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _createClient.apply(this, arguments);
}

export function updateClient(_x5) {
  return _updateClient.apply(this, arguments);
}
/**
 * 删除
 * @async
 * @function deleteClient
 * @param {object} params - 参数
 */
// export async function deleteClient(params) {
//   const { tenantId } = params;
//   return request(`${prefix}/${tenantId}/clients`, {
//     method: 'DELETE',
//     // body: params,
//   });
// }

/**
 * 查询当前登录用户所拥有 分配 全选的 角色
 * @param {!Number} currentOrganizationId 租户id
 */

function _updateClient() {
  _updateClient = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(params) {
    var tenantId, others;
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            tenantId = params.tenantId, others = _objectWithoutProperties(params, _excluded4);
            return _context5.abrupt("return", request("".concat(prefix, "/").concat(clientApi()), {
              method: 'PUT',
              body: others
            }));

          case 2:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _updateClient.apply(this, arguments);
}

export function subAccountOrgRoleQueryAll(_x6) {
  return _subAccountOrgRoleQueryAll.apply(this, arguments);
}
/**
 * 查询当前租户下的用户所拥有的角色
 * @param {!Number} currentOrganizationId 租户id
 * @param {!Number} userId 用户id
 */

function _subAccountOrgRoleQueryAll() {
  _subAccountOrgRoleQueryAll = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(payload) {
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            return _context6.abrupt("return", request("".concat(CPAAS_IAM, "/cpaas/v1/roles/self/user-assignable-roles"), {
              method: 'GET',
              query: payload
            }));

          case 1:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _subAccountOrgRoleQueryAll.apply(this, arguments);
}

export function subAccountOrgRoleCurrent(_x7) {
  return _subAccountOrgRoleCurrent.apply(this, arguments);
}
/**
 * 保存角色
 * @async
 * @function createClient
 * @param {object} params - 创建参数
 */

function _subAccountOrgRoleCurrent() {
  _subAccountOrgRoleCurrent = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(params) {
    var clientId, others, parsedParams;
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            clientId = params.clientId, others = _objectWithoutProperties(params, _excluded5);
            parsedParams = parseParameters(others);
            return _context7.abrupt("return", request("".concat(CPAAS_IAM, "/cpaas/v1/").concat(client(), "member-roles/client-roles/").concat(clientId), {
              method: 'GET',
              query: parsedParams
            }));

          case 3:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _subAccountOrgRoleCurrent.apply(this, arguments);
}

export function saveRoleSet(_x8) {
  return _saveRoleSet.apply(this, arguments);
}
/**
 * 删除角色
 * @param {Number} memberRoleList 用户角色列表
 */

function _saveRoleSet() {
  _saveRoleSet = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(params) {
    return _regeneratorRuntime.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            return _context8.abrupt("return", request("".concat(CPAAS_IAM, "/cpaas/v1/").concat(client(), "/member-roles/batch-assign"), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  }));
  return _saveRoleSet.apply(this, arguments);
}

export function deleteRoles(_x9) {
  return _deleteRoles.apply(this, arguments);
}
/**
 * 查询当前租户下的用户所拥有的角色
 * @param {!Number} currentOrganizationId 租户id
 * @param {!Number} userId 用户id
 */

function _deleteRoles() {
  _deleteRoles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(memberRoleList) {
    return _regeneratorRuntime.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            return _context9.abrupt("return", request("".concat(CPAAS_IAM, "/cpaas/v1/").concat(client(), "/member-roles"), {
              method: 'DELETE',
              body: memberRoleList
            }));

          case 1:
          case "end":
            return _context9.stop();
        }
      }
    }, _callee9);
  }));
  return _deleteRoles.apply(this, arguments);
}

export function roleVisitCurrent(_x10) {
  return _roleVisitCurrent.apply(this, arguments);
}
/**
 * 保存角色
 * @async
 * @function createClient
 * @param {object} params - 创建参数
 */

function _roleVisitCurrent() {
  _roleVisitCurrent = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(params) {
    var clientId, others, parsedParams;
    return _regeneratorRuntime.wrap(function _callee10$(_context10) {
      while (1) {
        switch (_context10.prev = _context10.next) {
          case 0:
            clientId = params.clientId, others = _objectWithoutProperties(params, _excluded6);
            parsedParams = parseParameters(others);
            return _context10.abrupt("return", request("".concat(CPAAS_IAM, "/v1/").concat(visitRole(), "/").concat(clientId), {
              method: 'GET',
              query: parsedParams
            }));

          case 3:
          case "end":
            return _context10.stop();
        }
      }
    }, _callee10);
  }));
  return _roleVisitCurrent.apply(this, arguments);
}

export function saveVisitRoleSet(_x11) {
  return _saveVisitRoleSet.apply(this, arguments);
}
/**
 * 删除角色
 * @param {Number} memberRoleList 用户角色列表
 */

function _saveVisitRoleSet() {
  _saveVisitRoleSet = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11(params) {
    var clientId, memberRoleList;
    return _regeneratorRuntime.wrap(function _callee11$(_context11) {
      while (1) {
        switch (_context11.prev = _context11.next) {
          case 0:
            clientId = params.clientId, memberRoleList = params.memberRoleList;
            return _context11.abrupt("return", request("".concat(CPAAS_IAM, "/v1/").concat(visitRole(), "/").concat(clientId), {
              method: 'POST',
              body: memberRoleList
            }));

          case 2:
          case "end":
            return _context11.stop();
        }
      }
    }, _callee11);
  }));
  return _saveVisitRoleSet.apply(this, arguments);
}

export function deleteVisitRoles(_x12) {
  return _deleteVisitRoles.apply(this, arguments);
}

function _deleteVisitRoles() {
  _deleteVisitRoles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee12(params) {
    var clientId, memberRoleList;
    return _regeneratorRuntime.wrap(function _callee12$(_context12) {
      while (1) {
        switch (_context12.prev = _context12.next) {
          case 0:
            clientId = params.clientId, memberRoleList = params.memberRoleList;
            return _context12.abrupt("return", request("".concat(CPAAS_IAM, "/v1/").concat(visitRole(), "/").concat(clientId), {
              method: 'DELETE',
              body: memberRoleList
            }));

          case 2:
          case "end":
            return _context12.stop();
        }
      }
    }, _callee12);
  }));
  return _deleteVisitRoles.apply(this, arguments);
}